package cn.itcast.banart.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.itcast.banart.dto.BanArtGoodsList;
import cn.itcast.banart.dto.BanArtOrder;
import cn.itcast.controller.IpBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 昆仑
 */
@RestController
@Slf4j
public class BanArtController {
    private static List<IpBean> list=new ArrayList<>();
    private static List<String> tokenList=new ArrayList<>();
    public  static volatile Map<String,IpBean> ret=new HashMap<>();
    @RequestMapping("/createBanartMarket")
    public String createBanartMarket(@RequestParam(value = "num",defaultValue = "10000") Integer num,
                               @RequestParam(value = "salePrice",defaultValue = "29") String salePrice,
                               @RequestParam(value = "goodsId",defaultValue = "24") String goodsId) {
        if(CollectionUtils.isEmpty(tokenList)){
            addToken();
        }
        for(int i=0;i<num;i++){
            try {
                    for(int z=0;z<tokenList.size();z++){
                        Proxy proxy=null;
                        confireMarketOrder(proxy,tokenList.get(z),goodsId,salePrice);
                    }

            } catch (Exception e) {
                log.error("#####异常####" + e.getMessage());
            }
        }
        log.info("####草球执行完成#####");
        return null;
    }
    public void confireMarketOrder(Proxy proxy,String token,String goodsId,String salePrice){
        try{
            createMarketOrder(proxy,token,goodsId,salePrice);
        }catch (Exception e){
            log.error("#####获取商品数据异常####"+e.getMessage()+"###"+token);
        }
    }
    public void createMarketOrder(Proxy proxy,String token,String goodsId,String salePrice) throws InterruptedException {
        String result=null;
        try{
            JSONObject requestJson = new JSONObject();
            requestJson.putOnce("sortNo", "0");
            requestJson.putOnce("collectionId", goodsId);
            requestJson.putOnce("page", "1");
            requestJson.putOnce("limit", "100");
            String requestParam = requestJson.toString();
            result =
                    HttpRequest.post("https://api.lemeta.group/collection/market/getSalesGoodss").body(requestParam)
                            .header("Authorization",token)
                    .setReadTimeout(5000)
                    .execute().body();
        }catch (Exception e){
            log.info("####请求商品数据异常######"+e.getMessage()+"####"+result);
        }
        if(!StringUtils.isEmpty(result)){
            BanArtGoodsList
                    banArtGoodsList = JSONUtil.toBean(result, BanArtGoodsList.class);
            log.info("######获取商品数据成功#######"+ banArtGoodsList.toString()+"##商品id###"+goodsId);
            if(banArtGoodsList.getData()!=null && !CollectionUtils.isEmpty(banArtGoodsList.getData().getList())){
                List<BanArtGoodsList.BanArtGoodsDetailList> list= banArtGoodsList.getData().getList();
                for(BanArtGoodsList.BanArtGoodsDetailList detailDTO:list){
                    if(new BigDecimal(detailDTO.getPrice()).compareTo(new BigDecimal(salePrice))<=0){
                        Proxy finalProxy = proxy;
                        ThreadUtil.execAsync(() -> {
                            saveOrder(finalProxy,token,detailDTO,goodsId);
                        });
                        Thread.sleep(3000);
                    }

                }
            }
        }else{
            log.info("##获取商品数据异常##");
        }
    }

    public void saveOrder(Proxy proxy, String token, BanArtGoodsList.BanArtGoodsDetailList detailDTO, String goodsId){

        String createResult=null;
        try {
            createResult =
                    HttpRequest.get("https://api.lemeta.group/collection/market/buy?id="+detailDTO.getId()).header("Authorization",token).execute().body();
            BanArtOrder banArtOrder = JSONUtil.toBean(createResult, BanArtOrder.class);
            log.info("####支付结果######"+ banArtOrder.toString());
            if(banArtOrder !=null && banArtOrder.getCode() ==0){
                MailUtil.send("1027060531@qq.com", "banArt支付", "您有一个banArt的订单，请赶紧支付！", false);
                log.info("####支付结果######"+ banArtOrder.toString());
            }
        } catch (Exception e) {
            log.info("####支付结果异常######"+e.getMessage()+":detailDTO"+detailDTO.toString());
        }
    }
    public void addToken(){
        //15182211372
//        tokenList.add("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvaDUuY2FvcWl1LnZpcFwvIiwiYXVkIjoiaHR0cHM6XC9cL2g1LmNhb3FpdS52aXBcLyIsImlhdCI6MTY4NjQ5NTMzNSwibmJmIjoxNjg2NDk1MzM1LCJleHAiOjE2ODY1ODE3MzUsIm1lbWJlcl9pbmZvIjo0Njc5NH0.SCQIG0rTfAXJ3kcl42le4WhNfIfKRh8GwvpdbHZTwJE");
        //15689276016
//        tokenList.add("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvaDUuY2FvcWl1LnZpcFwvIiwiYXVkIjoiaHR0cHM6XC9cL2g1LmNhb3FpdS52aXBcLyIsImlhdCI6MTY4NjQ5NTM3MSwibmJmIjoxNjg2NDk1MzcxLCJleHAiOjE2ODY1ODE3NzEsIm1lbWJlcl9pbmZvIjo0NjM0N30.gugK3Z3Qoim700eotxcPuUBGbWgIUmy0rnSEbsvVySU");
        //13151506052
//        tokenList.add("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvaDUuY2FvcWl1LnZpcFwvIiwiYXVkIjoiaHR0cHM6XC9cL2g1LmNhb3FpdS52aXBcLyIsImlhdCI6MTY4NjQ5NTQwMSwibmJmIjoxNjg2NDk1NDAxLCJleHAiOjE2ODY1ODE4MDEsIm1lbWJlcl9pbmZvIjo0NjM1MX0.Y3aL_Eps8CwazORaLNIQaPsRiSzOGNo2yMS5Pb9Qw9s");
        //13803993919
//        tokenList.add("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvaDUuY2FvcWl1LnZpcFwvIiwiYXVkIjoiaHR0cHM6XC9cL2g1LmNhb3FpdS52aXBcLyIsImlhdCI6MTY4NjQ4NjkxMywibmJmIjoxNjg2NDg2OTEzLCJleHAiOjE2ODY1NzMzMTMsIm1lbWJlcl9pbmZvIjo0NjM0OX0.c84jUZyXrGA5UGOty3cZcY_Jb-VBGL_g0vsSbuEKwOg");
//        //18883759475
//        tokenList.add("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvaDUuY2FvcWl1LnZpcFwvIiwiYXVkIjoiaHR0cHM6XC9cL2g1LmNhb3FpdS52aXBcLyIsImlhdCI6MTY4NjQ5NTQzMCwibmJmIjoxNjg2NDk1NDMwLCJleHAiOjE2ODY1ODE4MzAsIm1lbWJlcl9pbmZvIjo0NjM0Nn0.IK2uXkvjdrsKN_AycUxowcFXcyZvYwUfzvxDxgd6J7Q");
        //13996955613
        tokenList.add("Bearer cda92a83-ad8b-4247-a550-d581364dd3a2");
    }

}
